package cn.itsource.blog.service.impl;

import cn.itsource.blog.domain.Param;
import cn.itsource.blog.mapper.ParamMapper;
import cn.itsource.blog.query.ParamQuery;
import cn.itsource.blog.service.IParamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 业务实现类：博客参数Param
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ParamServiceImpl implements IParamService {
    @Autowired
    private ParamMapper paramMapper;

    @Override
    public List<Param> findAll() {
        return paramMapper.findAll();
    }

    @Override
    public Param findOne(Long id) {
        return paramMapper.findOne(id);
    }

    @Transactional
    @Override
    public void add(Param param) {
        paramMapper.add(param);
    }

    @Transactional
    @Override
    public void update(Param param) {
        param.setUpdateTime(new Date());
        paramMapper.update(param);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        paramMapper.delete(id);
    }

    @Transactional
    @Override
    public void patchDel(Long[] ids) {
        paramMapper.patchDel(ids);
    }

    @Override
    public PageInfo<Param> queryPage(ParamQuery paramQuery) {
        //开启分页
        PageHelper.startPage(paramQuery.getCurrentPage(),paramQuery.getPageSize());
        //1.首先会查询总条数
        //2.会根据方言或驱动名进行分页查询数据 - mysql - limit
        //3.会自动计算limit的第一个参数

        List<Param> list = paramMapper.queryPage(paramQuery);
        //调用PageInfo的有参会计算其他参数
        PageInfo<Param> pageInfo = new PageInfo<Param>(list);
        return pageInfo;
    }

    @Override
    public Map<String, String> getParams() {
        List<Param> params = paramMapper.findAll();
        return params.stream().collect(Collectors.toMap(Param::getParamKey,Param::getParamValue));

       /* Map<String, String> map = new HashMap<>();
        for (Param param : params) {
            map.put(param.getParamKey(),param.getParamValue());
        }
        return map;*/
    }
}
